<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
  <title>script.aculo.us Unit test file</title>
  <meta http-equiv="content-type" content="text/html; charset=utf-8" />
  <script src="javascripts/prototype.js" type="text/javascript"></script>
  <script src="javascripts/scal.js" type="text/javascript"></script>
  <script src="javascripts/unittest.js" type="text/javascript"></script>
  <link rel="stylesheet" href="styles/test.css" type="text/css" />
  <link rel="stylesheet" href="styles/scal.css" type="text/css" />
</head>
<body>
<h1>script.aculo.us Unit test file</h1>
<p>
  Tests for scal calendar.
</p>

<!-- Log output -->
<div id="testlog"> </div>

<input type="text" id="samplecal_value" name="samplecal_value" />
<div id="samplecal" class="dashblack" style="display: none;"></div>
<div id="samplecal2" class="dashblack" style="display: none;"></div>
<!-- Tests follow -->
<script type="text/javascript" language="javascript" charset="utf-8">
// <![CDATA[
    var upd = function(d){ $('samplecal_value').value = d.format('mm-dd-yyyy'); };

    var options = ({titleformat:'mmmm yyyy',
                closebutton:'Y',
                dayheadlength:2,
                weekdaystart:0,
                month:5,
                year:2020,
                day:4,
                dayheadlength:3,
                prevbutton: 'P',
								nextbutton: 'N',
								daypadding: true
    });  

new Test.Unit.Runner({
  testScal: function() { with(this) {
						$('samplecal').show();
            var Cal = new scal('samplecal',upd, options);
            assertInstanceOf(scal, Cal); 
            // init date test based on options
            assertEqual('May 2020', $('samplecal').select('.caltitle')[0].innerHTML);
            // day select same as option day
            assertEqual(4, $('samplecal').select('.dayselected')[0].down().innerHTML);
            
            // make sure we have 6 weeks
            var days = $('samplecal').select('.dayboxdate');
            assertEqual(42,days.size());
            assertEqual('Sun,Mon,Tue,Wed,Thu,Fri,Sat', $('samplecal').select('.dayboxname').collect(function(h){ return h.innerHTML; }).toString());
            // make sure all day values are numeric
						days.collect(function(h){ return h.innerHTML.strip(); }).each(function(val) {
									assertMatch(/^\d+$/,val);
									assertEqual(2, val.length);
            });
            assertEqual('N', $('samplecal').select('.calnextmonth')[0].innerHTML);
            assertEqual('P', $('samplecal').select('.calprevmonth')[0].innerHTML);
	    			assertEqual('Y', $('samplecal').select('.calclose')[0].innerHTML);
	  }},
	 testControls: function() { with(this) {
            Event.simulateMouse($('samplecal').select('.calnextmonth')[0], 'click');
            assertEqual('June 2020', $('samplecal').select('.caltitle')[0].innerHTML);
            
            // make sure we have 6 weeks
            var days = $('samplecal').select('.dayboxdate');
            assertEqual(42,days.size());
            var prev = $('samplecal').select('.calprevmonth')[0];
            Event.simulateMouse(prev, 'click'); // May
            Event.simulateMouse(prev, 'click'); // April
            Event.simulateMouse(prev, 'click'); // March
            Event.simulateMouse(prev, 'click'); // January
            Event.simulateMouse(prev, 'click'); // Febuary
            Event.simulateMouse(prev, 'click'); // December
            assertEqual('December 2019', $('samplecal').select('.caltitle')[0].innerHTML);
            Event.simulateMouse($('samplecal').select('.calclose')[0], 'click');
						assertNotVisible($('samplecal'));
						$('samplecal').show();
       }},
	 testMethods: function() { with(this) {
            options.month = 1; // january test (thx pug)
            options.year = 2007;
						var Cal2 = new scal('samplecal2',upd,options);					
            assertInstanceOf(scal, Cal2); // multiple calendar test 
						assertEqual('January 2007', $('samplecal2').select('.caltitle')[0].innerHTML);
						Cal2.toggleCalendar();
      			wait(100, function() {
							assertNotVisible($('samplecal2'));
							Cal2.toggleCalendar();
			      });
      			wait(100, function() {
							assertVisible($('samplecal2'));
			      });           	
            Cal2.destroy();
            assertEqual(0, $('samplecal2').descendants().size());
            var testYears = 2;   // change as needed         
            $R(1,testYears).each(function(y) {
                    var year = 2007 + y;
                    $R(1,12).each(function(m) {
                        var month = Date.prototype.monthnames[m - 1];
                        var assertionString = month + ' ' + year;
                        options.month = m;
                        options.year = year;
                        var Cal2 = new scal('samplecal2',upd,options);
                        assertInstanceOf(scal, Cal2); // multiple calendar test 
                        assertEqual(assertionString, $('samplecal2').select('.caltitle')[0].innerHTML);
                        Cal2.destroy();
                        assertEqual(0, $('samplecal2').descendants().size());
                    });
            });            
            var Cal2 = new scal('samplecal2',upd,options);
            assertInstanceOf(scal, Cal2); // multiple calendar test
            assertEqual(Cal2.setCurrentDate(new Date('February 12, 2017')).format('dd'), 12); // current date returns the current date
            assertEqual(12, $('samplecal2').select('.dayselected')[0].down().innerHTML);
						assertEqual('February 2017', $('samplecal2').select('.caltitle')[0].innerHTML);
						assertEqual('02-12-2017', $('samplecal_value').getValue());
            Cal2.setCurrentDate(new Date('September 30, 1974'), true);
            assertEqual(30, $('samplecal2').select('.dayselected')[0].down().innerHTML);
						assertEqual('September 1974', $('samplecal2').select('.caltitle')[0].innerHTML);
						assertEqual('02-12-2017', $('samplecal_value').getValue());
						Cal2.setCurrentDate('init');
            assertEqual(4, $('samplecal2').select('.dayselected')[0].down().innerHTML);
            var lastY = testYears + 2007;
            assertEqual('December ' + lastY, $('samplecal2').select('.caltitle')[0].innerHTML);
            Cal2.setCurrentDate('monthdown');
            assertEqual(4, $('samplecal2').select('.dayselected')[0].down().innerHTML);
            assertEqual('November ' + lastY, $('samplecal2').select('.caltitle')[0].innerHTML);
            Cal2.setCurrentDate('monthup');
            assertEqual('December ' + lastY, $('samplecal2').select('.caltitle')[0].innerHTML);
            assertEqual(4, $('samplecal2').select('.dayselected')[0].down().innerHTML);
            Cal2.setCurrentDate('yeardown');
            lastY -= 1;
            assertEqual(4, $('samplecal2').select('.dayselected')[0].down().innerHTML);
            assertEqual('December ' + lastY, $('samplecal2').select('.caltitle')[0].innerHTML);
            Cal2.setCurrentDate('yearup');
            lastY += 1;
            assertEqual('December ' + lastY, $('samplecal2').select('.caltitle')[0].innerHTML);
            assertEqual(4, $('samplecal2').select('.dayselected')[0].down().innerHTML);
            var first = Cal2.getElementByDate(new Date('November 29, 2009'));
            var firstToCompare = $('samplecal2').select('.cal_day_0_0')[0];
            assertEqual(first.inspect(),firstToCompare.inspect());
            first = Cal2.getElementsByWeek(0);
            firstToCompare = $('samplecal2').select('.weekbox')[1].select('.daybox');
            assertEqual(first.inspect(),firstToCompare.inspect());
            first = Cal2.getSelectedElement();
            firstToCompare = $('samplecal2').select('.dayselected')[0];
            assertEqual(first.inspect(),firstToCompare.inspect());
            firstToCompare = new Date();
            firstToCompare.setHours(0,0,0,0);
            Cal2.setCurrentDate(firstToCompare);
            first = Cal2.getDateByElement($('samplecal2').select('.dayselected')[0]);
						assertEqual(first.toJSON(), firstToCompare.toJSON());
            Cal2.destroy();
            assertEqual(0, $('samplecal2').descendants().size())
			}},  
			testClasses: function() { with(this) {		
          var Cal2 = new scal('samplecal2',upd,options);
          assertInstanceOf(scal, Cal2); // multiple calendar tes
    		  // test classnames
					var eWeeks = $('samplecal2').select('.endweek');
					assertNotEqual(eWeeks.size(), 0, 'Endweek check');
          eWeeks.each(function(sat){
                if(!sat.hasClassName('dayboxname')) {
                    assertEqual('Saturday', Cal2.getDateByElement(sat).format('dddd'));
                }
					});
					sundays = $('samplecal2').select('.dayboxsunday');
					assertNotEqual(sundays.size(), 0, 'Sunday check');
          sundays.each(function(sun){
                    assertEqual('Sunday', Cal2.getDateByElement(sun).format('dddd'));
					});
          
        }}

    
 
  });
// ]]>
</script>
</body>
</html>

